package tjs.styudy.mini.springboot.demo.transaction;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import tjs.styudy.mini.springboot.demo.transaction.service.TransactionalService;
import tjs.styudy.mini.springboot.transaction.aop.TransactionAspect;

/**
 * Created by Nicky.Tang on 2021/10/27 9:35 下午
 *
 * @since 02.12.10
 */
@SpringBootApplication
public class DoTestOfAppplaction {
    /**
     * 切面类：{@link tjs.styudy.mini.springboot.transaction.aop.TransactionAspect#around(org.aspectj.lang.ProceedingJoinPoint)}
     * 提交事务：{@link tjs.styudy.mini.springboot.transaction.config.TransactionManager#commit(tjs.styudy.mini.springboot.transaction.config.TransactionInfo)}
     * 回滚事务：{@link tjs.styudy.mini.springboot.transaction.config.TransactionManager#rollBack(tjs.styudy.mini.springboot.transaction.config.TransactionInfo)}
     * 清理资源：{@link tjs.styudy.mini.springboot.transaction.config.TransactionManager#cleanupAfterCompletion()}
     * 获取当前连接：{@link tjs.styudy.mini.springboot.transaction.config.ConnectionHolder#getResource()}
     */
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DoTestOfAppplaction.class);

        testUpdate(context);
    }

    private static void testUpdate(AnnotationConfigApplicationContext context) {
        TransactionalService transactionalService = context.getBean(TransactionalService.class);
        transactionalService.find("before add");
        try {
            transactionalService.updateSonOfBalance(false);
            //transactionalService.updateFatherOfUserName(true);
        }catch (Exception ex){
            ex.printStackTrace();
            System.out.println("add fail, end rollback...");
        }
        transactionalService.find("after add");
    }

    @Bean("dataSource")
    public BasicDataSource initBasicDataSource() {
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setUrl("jdbc:mysql://localhost:3306/tjs_mydemo");
        basicDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("qx123456");
        return basicDataSource;
    }
}
